// TODO: 2025/5/27
package Practice.shuffle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardsDemo {
     public String[] suits = {"♥","♠","♣","♦"};//四种花色

     //1.买52张牌
     public List<Cards> buyCards(){
          List<Cards> cardsList = new ArrayList<>();
          for (int i = 1; i <= 13; i++) {
               for (int j = 0; j < 4; j++) {
                    int rank = i;
                    String suit = suits[j];
                    Cards cards = new Cards(suit,rank);
                    cardsList.add(cards);
               }
          }
          return cardsList;
     }


     //2.洗牌
     public void shuffle(List<Cards> cardsList){
          Random random = new Random();
          for (int i = cardsList.size()-1; i > 0; i--) {
               int index = random.nextInt(i);//返回到是0-51的之间的随机数字
               swap(cardsList,i,index);
          }
     }
     private void swap(List<Cards> cardsList,int i,int j){//
          /*
          Cards temp = CardList[i];
          CardsList[i] = CardsList[j];
          CardsList[j] = temp;
           */
          Cards temp = cardsList.get(i);
          cardsList.set(i,cardsList.get(j));
          cardsList.set(j,temp);
     }

     //3.3人玩牌
     //买个人都持有一套牌，所以用ArrayList
     public List<List<Cards>> play(List<Cards> cardsList){

          List<Cards> hand0 = new ArrayList<>();
          List<Cards> hand1 = new ArrayList<>();
          List<Cards> hand2 = new ArrayList<>();
          List<List<Cards>> hand = new ArrayList<>();
          hand.add(hand0);
          hand.add(hand1);
          hand.add(hand2);
          for (int i = 0; i < 5; i++) {
               for (int j = 0; j < 3; j++) {//3个人，每人拿1张
                    Cards cards = cardsList.remove(0);//每人拿完一张牌，那么再重新拿牌，依旧是剩下的里面的第一张牌
                    hand.get(j).add(cards);
               }
          }

          return hand;
     }
}
